<template>
  <div class="WorkingSchedule" v-loading="loading">
    <el-form ref="form" :model="form" label-position="top" class="myform">
      <div>
        <el-form-item label="采购单号" class="myform-item">
          <el-input
            v-model="form.po_id"
            placeholder="请输入采购单号"
            clearable
            @keyup.enter.native="onSearch"
          ></el-input>
        </el-form-item>
        <el-form-item label="工厂名称" class="myform-item">
          <el-input
            v-model="form.factory_name"
            placeholder="请输入工厂名称"
            clearable
            @keyup.enter.native="onSearch"
          ></el-input>
        </el-form-item>
      </div>
      <div>
        <el-form-item style="margin-top: 50px">
          <el-button type="primary" @click="onSearch">查询</el-button>
        </el-form-item>
      </div>
    </el-form>
    <el-table :data="tableData" style="width: 100%" border>
      <el-table-column
        label="入库回货单号"
        align="center"
        prop="io_id"
      ></el-table-column>
      <el-table-column
        label="供应商名称"
        align="center"
        prop="supplier_name"
      ></el-table-column>
      <el-table-column
        label="采购单号"
        align="center"
        prop="po_id"
      ></el-table-column>
      <el-table-column
        label="SKU"
        align="center"
        prop="sku_id"
      ></el-table-column>
      <el-table-column label="数量" align="center" prop="qty"></el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="{ row }">
          <div v-if="row.po_id">
            <el-button type="text" size="default" @click="onExisting()"
              >已存在加工单</el-button
            >
            <el-button
              type="text"
              size="default"
              style="color: red"
              @click="onExistingAdd(row.po_id)"
              >未存在加工单</el-button
            >
          </div>
          <div v-else>
            <el-button
              type="text"
              size="default"
              @click="onProcurement(row.io_id,row.sku_id)"
              >采购单号修正</el-button
            >
          </div>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      :form="form"
      :total="total"
      @paginationPostOrderTaskList="postAdminPricingGetNoPurchaseinList"
    />
  </div>
</template>

<script>
import {
  postAdminPricingGetNoPurchaseinList,
  postAdminPricingGetProcessing_code_you,
  postAdminPricingSetPurchaseinOrder,
} from "@/api";
import pagination from "@/components/pagination.vue";
export default {
  components: { pagination },
  data() {
    return {
      loading: false,
      form: {
        po_id: "",
        factory_name: "",
        page: 1,
        size: 10,
      },
      tableData: [],
      total: 1,
    };
  },
  activated() {
    let form = this.form;
    this.postAdminPricingGetNoPurchaseinList(form);
  },
  methods: {
    // 未存在的加工单
    onExistingAdd(po_id) {
      this.$router.push({ name: "contractcorrectionadd", query: { po_id } });
    },
    //采购单修正
    onProcurement(io_id,sku_id) {
      this.$prompt("采购单号修正", "采购单号", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        inputValidator: (value) => {
          if (value) return true;
          return false;
        },
        inputPlaceholder: "请输入采购单号",
        inputErrorMessage: "请输入采购单号",
      })
        .then(({ value }) => {
          let obj = {
            io_id,
            po_id: value,
            sku_id
          };
          postAdminPricingSetPurchaseinOrder(obj).then((res) => {
            this.$message({
              type: "success",
              message: "采购单号修正成功",
            });
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "取消输入",
          });
        });
    },
    // 已存在加工单
    onExisting() {
      this.$prompt("生产加工单验证", "生产加工单号", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        inputValidator: (value) => {
          if (value) return true;
          return false;
        },
        inputPlaceholder: "请输入生产加工单号",
        inputErrorMessage: "请输入生产加工单号",
      })
        .then(({ value }) => {
          let obj = {
            processing_code: value,
          };
          postAdminPricingGetProcessing_code_you(obj).then((res) => {
            this.$message({
              type: "success",
              message: "查询有此加工单，请去数加同步！",
            });
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "取消输入",
          });
        });
    },
    // 采购列表
    async postAdminPricingGetNoPurchaseinList(data) {
      this.loading = true;
      this.form = data;
      postAdminPricingGetNoPurchaseinList(data)
        .then((res) => {
          this.loading = false;
          console.log("采购列表", res);
          this.tableData = res.data;
          this.total = Number(res.total);
        })
        .catch((err) => {
          this.loading = false;
        });
    },
    // 搜索
    onSearch() {
      this.form.page = 1;
      this.form.size = 10;
      this.postAdminPricingGetNoPurchaseinList(this.form);
    },
  },
};
</script>

<style lang="scss" scoped>
.myform {
  display: flex;
  justify-content: space-between;
  > div {
    display: flex;
  }
  &-item {
    margin-right: 10px;
  }
}
.WorkingSchedule {
  background: #ffff;
  padding: 20px;
  border-radius: 2px;
  @include boxShadow;
}
</style>